<?php
/**
 * @title 622. 设计循环队列 - 指针
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class MyCircularQueue {
    /**
     * Initialize your data structure here. Set the size of the queue to be k.
     * @param Integer $k
     */
    function __construct($k) {
        $this->maxlen = $k;
        $this->list = [];
        $this->len = 0;
        $this->point = 0;
    }

    /**
     * Insert an element into the circular queue. Return true if the operation is successful.
     * @param Integer $value
     * @return Boolean
     */
    function enQueue($value) {
        /**
         * @since 2020-09-21 enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真
         */
        if($this->len < $this->maxlen){
            $this->list[] = $value;
            $this->len++;
            return true;
        } else {
            return false;
        }
    }

    /**
     * Delete an element from the circular queue. Return true if the operation is successful.
     * @return Boolean
     */
    function deQueue() {
        /**
         * @since 2020-09-21 从循环队列中删除一个元素。如果成功删除则返回真
         */
        if($this->len >= 1){
            unset($this->list[$this->point]);
            $this->point++;
            $this->len--;
            return true;
        } else {
            return false;
        }
    }

    /**
     * Get the front item from the queue.
     * @return Integer
     */
    function Front() {
        /**
         * @since 2020-09-21 Front: 从队首获取元素。如果队列为空，返回 -1
         */
        if($this->len >= 1){
            return $this->list[$this->point];
        } else {
            return -1;
        }
    }

    /**
     * Get the last item from the queue.
     * @return Integer
     */
    function Rear() {
        /**
         * @since 2020-09-21 Rear: 获取队尾元素。如果队列为空，返回 -1
         */
        if($this->len >= 1){
            $key = $this->point + $this->len -1;
            return $this->list[$key];
        } else {
            return -1;
        }
    }

    /**
     * Checks whether the circular queue is empty or not.
     * @return Boolean
     */
    function isEmpty() {
        return $this->len == 0;
    }

    /**
     * Checks whether the circular queue is full or not.
     * @return Boolean
     */
    function isFull() {
        return $this->len == $this->maxlen;
    }
}

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * $obj = MyCircularQueue($k);
 * $ret_1 = $obj->enQueue($value);
 * $ret_2 = $obj->deQueue();
 * $ret_3 = $obj->Front();
 * $ret_4 = $obj->Rear();
 * $ret_5 = $obj->isEmpty();
 * $ret_6 = $obj->isFull();
 */

/**
 * @url http://127.0.0.1/leetcode/202009/2020.09.20_2.php
 */
$datas = [
    // [
    //     ["MyCircularQueue","enQueue","enQueue","enQueue","enQueue","Rear","isFull","deQueue","enQueue","Rear"],
    //     [[3],[1],[2],[3],[4],[],[],[],[4],[]]
    // ],

    [
        ["MyCircularQueue","enQueue","enQueue","enQueue","deQueue","Rear","deQueue","Rear"],
        [[3],[1],[2],[3],[],[],[],[]],
    ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
// foreach ($datas as $data){
//     var_dump($data);
//
//     $obj = new Solution();
//     $result = $obj->(...$data);
//     // $result = $obj->($xhprof->tree($data));
//     // $result = $obj->find(...$data);
//     var_dump($result);
//     // if(count($result)<=20){
//     //     var_dump($result);
//     // } else {
//     //     var_dump(count($result));
//     // }
//     echo str_repeat("<br>", 3);
// }

foreach ($datas as $data){
    $obj = new $data[0][0](...$data[1][0]);

    for ($i=1; $i<count($data[0]); $i++){
        $result = $obj->$data[0][$i](...$data[1][$i]);

        echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
        if($result === true){
            echo "True";
        } elseif($result === false) {
            echo "False";
        } elseif($result === null) {
            echo "Null";
        } elseif(is_array($result)) {
            var_dump($result);
        } else {
            echo $result;
        }
        echo PHP_EOL;
    }

    echo str_repeat(PHP_EOL, 3);
}
$xhprof->end();
